
#include <gtk/gtk.h>

#include "quickconnect_widget.h"

/* FIXME: Use gettext! */
#define _(a) (a)

#define MAX_TCP_PORT 65535
#define QUICK_CONNECT_DEFAULT_PORT 22

void on_quick_connect_entry_changed (GtkEditable*, gpointer);

G_DEFINE_TYPE (LsmQuickConnect, lsm_quickconnect, GTK_TYPE_HBOX)

static void
lsm_quickconnect_class_init (LsmQuickConnectClass *class)
{
	GtkWidgetClass *widget_class;

	widget_class = GTK_WIDGET_CLASS (class);

}

static void
lsm_quickconnect_init (LsmQuickConnect *quickconnect)
{

  GtkWidget *hbox;
  GtkWidget *label;
  GtkObject *adjustment;

  GtkWidget *qc_host_entry;
  GtkWidget *qc_user_entry;
  GtkWidget *qc_passwd_entry;
  GtkWidget *qc_spin_button;
  GtkWidget *qc_combo_box;
  GtkWidget *qc_button;

  hbox = GTK_WIDGET (quickconnect);
  
  gtk_box_set_homogeneous (GTK_BOX (quickconnect), FALSE);
  gtk_box_set_spacing (GTK_BOX (quickconnect), FALSE);


  gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);

/* */

  qc_host_entry = gtk_entry_new ();
  
  gtk_entry_set_width_chars (GTK_ENTRY (qc_host_entry), 18);
  
  g_signal_connect (G_OBJECT (qc_host_entry), "changed",\
    G_CALLBACK (on_quick_connect_entry_changed), hbox);
  
  label = gtk_label_new_with_mnemonic (_("_Host :"));
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), qc_host_entry);
  
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), qc_host_entry, FALSE, FALSE, 0);
  
/* */

  qc_user_entry = gtk_entry_new ();
  
  gtk_entry_set_width_chars (GTK_ENTRY (qc_user_entry), 12);
  
  g_signal_connect (G_OBJECT (qc_user_entry), "changed",\
    G_CALLBACK (on_quick_connect_entry_changed), hbox);
  
  label = gtk_label_new_with_mnemonic (_("_User :"));
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), qc_user_entry);
  
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), qc_user_entry, FALSE, FALSE, 0);
  
  
/* */

  qc_passwd_entry = gtk_entry_new ();
  
  gtk_entry_set_width_chars (GTK_ENTRY (qc_passwd_entry), 14);
  gtk_entry_set_visibility (GTK_ENTRY (qc_passwd_entry), FALSE);

  g_signal_connect (G_OBJECT (qc_passwd_entry), "changed",\
    G_CALLBACK (on_quick_connect_entry_changed), hbox);

  label = gtk_label_new_with_mnemonic (_("_Password :"));
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), qc_passwd_entry);
  
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), qc_passwd_entry, FALSE, FALSE, 0);
  
  
/* */

  adjustment = gtk_adjustment_new (QUICK_CONNECT_DEFAULT_PORT,\
                                   1,\
                                   MAX_TCP_PORT,
                                   1,\
                                   10,\
                                   0);

  qc_spin_button = gtk_spin_button_new (GTK_ADJUSTMENT (adjustment), 0, 0);

  label = gtk_label_new_with_mnemonic (_("P_ort :"));
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), qc_spin_button);
  
  
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), qc_spin_button, FALSE, FALSE, 0);

/* */

  qc_combo_box = gtk_combo_box_new_text ();
  
  gtk_combo_box_append_text (GTK_COMBO_BOX (qc_combo_box), _("SSH"));
  gtk_combo_box_append_text (GTK_COMBO_BOX (qc_combo_box), _("Telnet"));
  gtk_combo_box_set_active (GTK_COMBO_BOX (qc_combo_box), 0);

  gtk_box_pack_start (GTK_BOX (hbox), qc_combo_box, FALSE, FALSE, 0);

/* */

  qc_button = gtk_button_new_with_mnemonic (_("_Quick connect"));
  gtk_widget_set_sensitive (qc_button, FALSE);

  gtk_box_pack_start (GTK_BOX (hbox), qc_button, FALSE, FALSE, 0);

  LSM_QUICKCONNECT (hbox)->host_entry = qc_host_entry;
  LSM_QUICKCONNECT (hbox)->user_entry = qc_user_entry;
  LSM_QUICKCONNECT (hbox)->passwd_entry = qc_passwd_entry;
  LSM_QUICKCONNECT (hbox)->spin_button = qc_spin_button;
  LSM_QUICKCONNECT (hbox)->combo_box = qc_combo_box;
  LSM_QUICKCONNECT (hbox)->button = qc_button;

}

void
on_quick_connect_entry_changed (GtkEditable *editable, gpointer user_data)
{

  GtkWidget *qc_host_entry;
  GtkWidget *qc_user_entry;
  GtkWidget *qc_passwd_entry;
  GtkWidget *qc_button;
  gboolean val;

  qc_host_entry = LSM_QUICKCONNECT (user_data)->host_entry;
  qc_user_entry = LSM_QUICKCONNECT (user_data)->user_entry;
  qc_passwd_entry = LSM_QUICKCONNECT (user_data)->passwd_entry;
  qc_button = LSM_QUICKCONNECT (user_data)->button;


  val = gtk_entry_get_text_length (GTK_ENTRY (qc_host_entry)) != 0 &&\
        gtk_entry_get_text_length (GTK_ENTRY (qc_user_entry)) != 0 &&\
        gtk_entry_get_text_length (GTK_ENTRY (qc_passwd_entry)) != 0;

  gtk_widget_set_sensitive (qc_button, val);

}

GtkWidget *
lsm_quickconnect_new (void)
{
	return g_object_new (LSM_TYPE_QUICKCONNECT, NULL);
}

